// pages/search/search.js
// pages/hot_book/hot_book.js
let util = require("../../utils/util.js");
Page({
    //分享
    onShareAppMessage: function () { },
    /**
     * 页面的初始数据
     */
    data: {
        detail: [],
        pageNumber: 1,
        readMoreFlag: false,
        searchValue:"",
        searchFlag:false
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },
    setValue:function(options){
        var type = options.currentTarget.dataset.type;
        var value = options.detail.value;
        this.setData({
            [type]: value
        });
    },
    //获取书籍列表
    getList: function (options,flag) {
        const that = this;
        let pageNumber = this.data.pageNumber;
        var userInfo = JSON.parse(wx.getStorageSync("userInfo"));
        if(flag){
            this.setData({
                detail:[]
            });
        }
        if(!!options){
            if(options.currentTarget.dataset.flag == "clear"){
                this.setData({
                    detail: []
                });
            }
        }
        let oldData = this.data.detail;
        var keyWords = this.data.searchValue;
        if (keyWords == ""){
            wx.showToast({
                title: '请输入搜索内容！',
                icon:"none"
            })
            return false;
        }
        this.setData({
            readMoreFlag: false
        });
        util.wxAjax("glBook/search", "post", {
            keyWords: keyWords,
            pageNum: pageNumber,
            adminid:userInfo.smAdmin.id
        }, function (res) {
            wx.hideNavigationBarLoading();
            wx.stopPullDownRefresh();
            wx.hideLoading();
            var data = res.data;
            if (!!data.data) {
                var jsonData = data.data;
                for (var i = 0; i < jsonData.length; i++) {
                    jsonData[i].about = util.repalceHtml(jsonData[i].about);
                }
                oldData = oldData.concat(jsonData);
                that.setData({
                    detail: oldData,
                    readMoreFlag: jsonData.length > 19 ? true : false,
                    searchValue: "",
                    searchFlag:false
                })
            }else{
                that.setData({
                    detail:[],
                    searchValue:"",
                    searchFlag:true
                });
                // wx.showToast({
                //     title: '暂没有信息~',
                //     icon:"none"
                // })
            }
        })
    },
    //上拉刷新
    onPullDownRefresh: function () {
        // 显示顶部刷新图标  
        wx.showNavigationBarLoading();
        this.setData({
            pageNumber: 1,
            detail: []
        });
        this.getList(null,true);
    },
    //下拉加载
    onReachBottom: function () {
        var flag = this.data.readMoreFlag;
        if (flag) {
            var pageNum1 = this.data.pageNumber;
            // 显示加载图标  
            wx.showLoading({
                title: '玩命加载中',
            })
            this.setData({
                pageNumber: ++pageNum1,
            });
            this.getList(null);
        }
    },
    //书籍点赞
    dianzan: function (options) {
        var that = this;
        var index = options.currentTarget.dataset.index;
        var detail = this.data.detail[index];
        var userInfo = JSON.parse(wx.getStorageSync("userInfo"));
        util.wxAjax("glBook/updateBookLaudsAddOne", "post", {
            adminid: userInfo.smAdmin.id,
            id: detail.id
        }, function (res) {
            var data = res.data;
            if (data.result == 200 || data.reslut == "200") {
                wx.showToast({
                    title: '点赞成功！',
                })
                detail.lauds = data.data;
                detail.isLauds = 1;
                that.setData({
                    ["detail[" + index + "]"]: detail
                });
            } else {
                wx.showToast({
                    title: data.msg,
                    icon: "none"
                })
            }
        });
    },
})